/**
 * 
 */
package wechat.service.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wechat.handler.bean.WechatInformationBean;
import wechat.handler.confirmation.Confirmation;
import wechat.handler.work.WechatInformationHandler;


@WebServlet("/WechatServlet")
public class WechatServlet extends HttpServlet {

	private static final long serialVersionUID = 4653044830625831984L;
	private Logger log = LoggerFactory.getLogger(WechatServlet.class);

	/**
	 * 微信服务器验证
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("WechatServlet Get");
		
		String appid = request.getAttribute("appid").toString();
		log.info("WechatServlet Get appid=" + appid);
		
		PrintWriter printWriter = response.getWriter();

		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		
		log.info("WechatServlet  signature=" + signature + " timestamp=" + timestamp + " nonce=" + nonce);
		
		if (signature == null && timestamp == null && nonce == null) {
			log.info("WechatServlet Request=" + request.getParameterMap());
			return;
		}
		
		if (Confirmation.confirm(appid,signature, timestamp, nonce)) {
			printWriter.write(echostr);
			printWriter.flush();
			printWriter.close();
		} else {
			System.out.println("WechatServlet : 服务器验证失败！");
			log.error("WechatServlet : 服务器验证失败！");
		}

	}

	/**
	 * 微信消息接收
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("WechatServlet Post");
		
		String appid = request.getAttribute("appid").toString();
		log.info("WechatServlet Post appid="+ appid);
		
		String encryptType = request.getParameter("encrypt_type");
		String msgSignature = request.getParameter("msg_signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		
		log.info("WechatServlet  encryptType=" + encryptType + " msgSignature=" + msgSignature + " nonce=" + nonce + " timestamp=" + timestamp);
		
		response.setCharacterEncoding("UTF-8");
		InputStream input = request.getInputStream();
	
		WechatInformationBean wechatInformationBean = new WechatInformationBean();
		wechatInformationBean.setDataInputStream(input);
		wechatInformationBean.setEncryptType(encryptType);
		wechatInformationBean.setMsgSignature(msgSignature);
		wechatInformationBean.setNonce(nonce);
		wechatInformationBean.setTimestamp(timestamp);
		
		WechatInformationHandler wechatInformationHandler = new WechatInformationHandler(appid);
		String process = wechatInformationHandler.process(appid,wechatInformationBean);
		
		PrintWriter printWriter = response.getWriter();
		printWriter.write(process);
		printWriter.flush();
		printWriter.close();
	}

}
